package bb.lanxing.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import bb.lanxing.App;

public class NetworkUtil {
    public static boolean isWifiConnected() {
        return isNetworkConnected(1);
    }

    public static boolean isMobileConnected() {
        return isNetworkConnected(0);
    }

    public static int getConnectedType(Context context) {
        NetworkInfo activeNetwork;
        if (context == null || (activeNetwork = getActiveNetwork(context)) == null || !activeNetwork.isAvailable()) {
            return -1;
        }
        return activeNetwork.getType();
    }

    private static boolean isNetworkConnected(int i) {
        NetworkInfo activeNetwork = getActiveNetwork(App.getContext());
        return activeNetwork != null && activeNetwork.getType() == i && activeNetwork.isConnected();
    }

    private static NetworkInfo getActiveNetwork(Context context) {
        return ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    }
}
